<?php

namespace app\index\controller;
use app\comm\controller\WorkController;
use app\comm\service\ChartService;
use app\comm\service\WorkService;
use think\Db;
class Work extends WorkController{

	/**
	 * liaotian 
	 * @return [type] [description]
	 */
	public function index(){
		$chartservice = new ChartService;
		$stroreid = request()->get('storeid');
		$list  = $chartservice ->getkefu($stroreid);
		$this->assign('list',$list);
		
		$roe = $this->userinfo;
		$this->assign('userinfo',$roe);
		//dd($this->userinfo);
		$roe['uid'] ="forent".$roe['uid'];
		$info 	= json_encode(['nickname'=>$roe['nickname'],"logo"=>$roe['logo'],'uid'=>$roe['uid']]);
		$this->assign('info',$info);
		//,['list'=>$list,"userinfo"=>$roe,"info"=>$info]
		return view('work/index');
	}

	/**
	 * 发送消息
	 */
	public function sendms(){
		$workservice = new WorkService();
		$client = request()->get("client");
		$msg    = request()->get("msg");
		$data   = ["msg"=>$msg,"type"=>2];
		$msgs   = json_encode($data);
		$this->workservice($client,$msgs);
	}

	/**
	 * 绑定uid
	 */
	public function binduidbyclient(){
		$workservice = new WorkService();
		$client = request()->get("client");
		$uid = request()->get("uid");
		$workservice->bindbyuid($client,$uid);
	}

	/**
	 * uid 发送消息 xiaoxi type =1
	 */
	public function sedmsuid(){
		$workservice = new WorkService();

		// username  :msg.username            //'二刀'
  //   	
  //  

		$fromuid  =  request()->get("fromuid");
		$touid    = request()->get("touid");
		$msg      = request()->get("msg");
		$type     =  request()->get("type");
		$userinfo = request()->get("userinfo");

		$isinsert = ['chat_uid'=>$fromuid,'admin_uid'=>$touid];
		$frontuser = Db::table('ys_chat_front_user')->where($isinsert)->find();
		if(empty($frontuser)){
			$isinsert['addtime'] = time();
			Db::table('ys_chat_front_user')->insert($isinsert);
		}
		$datamsg  = ['msg' => $msg,   'addtime'=>time(),  'type'=>1,  'userinfo'=>$userinfo];
		$datamsg ['sing']  =   $fromuid.$touid;		
		Db::table('ys_chat_front_msg')->insert($datamsg);
		if(!$workservice->isparonline($touid))  return "不在线";
		$data = ["fromuid"=>$fromuid,  "touid"=>$touid,  'msg'=>$msg ,    'type'    =>$type, 
				"userinfo"=>$userinfo, 'avatar'=>$this->userinfo['logo'], 'username'=>$this->userinfo['realname']
			  ];
		$workservice->sendmsg($touid,json_encode($data));
		return json($data);
	}

	/**
	 * 是否在线
	 */
	public function isonline(){
		$workservice = new WorkService();
		$touid = request()->get("touid");
		$result = $workservice->isparonline($touid);
		return $result;
	}

	/**
	 * 历史记录historicalrecord
	 */
	public function historicalrecord(){
		$sing = request()->get('sing');
		$historylist = Db::table('ys_chat_front_msg')->where('sing',$sing)->order('addtime desc')->page('1',30)->select();
		return json(["status"=>1,"msg"=>$historylist]);
	}

}